<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <base href="<%=request.getContextPath()%>/">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>完善个人信息 - 医疗预约系统</title>
    <link rel="stylesheet" href="statics/css/hisStyle.css">
    <style>
        body {
            background-color: #f5f7fa;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        .container {
            max-width: 800px;
            margin: 50px auto;
            padding: 40px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .header h1 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 28px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: bold;
            font-size: 14px;
        }
        .form-control {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
            box-sizing: border-box;
            transition: border-color 0.3s, box-shadow 0.3s;
        }
        .form-control:focus {
            outline: none;
            border-color: #3498db;
            box-shadow: 0 0 5px rgba(52, 152, 219, 0.3);
        }
        .btn-submit {
            width: 100%;
            padding: 12px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        .btn-submit:hover {
            background-color: #2980b9;
        }
        .error-message {
            color: #e74c3c;
            background-color: #fdf2f2;
            border: 1px solid #fecaca;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        .success-message {
            color: #27ae60;
            background-color: #f0f9f0;
            border: 1px solid #c3e6c3;
            padding: 10px;
            border-radius: 5px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        .form-row {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
        }
        .form-row .form-group {
            flex: 1;
            min-width: 250px;
        }
        .required-field {
            color: #e74c3c;
        }
        @media (max-width: 768px) {
            .container {
                margin: 30px 20px;
                padding: 30px;
            }
            .form-row {
                flex-direction: column;
            }
            .form-row .form-group {
                min-width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>完善个人信息</h1>
            <p>请填写以下信息以完成您的账号设置</p>
        </div>

        <!-- 错误提示 -->
        <c:if test="${not empty error}">
            <div class="error-message">
                ${error}
            </div>
        </c:if>

        <!-- 成功提示 -->
        <c:if test="${not empty success}">
            <div class="success-message">
                ${success}
            </div>
        </c:if>

        <form action="auth/updateProfile" method="post" id="profileForm">
            <input type="hidden" name="userId" value="${loginUser.userid}">
            
            <div class="form-row">
                <div class="form-group">
                    <label for="realName">真实姓名：<span class="required-field">*</span></label>
                    <input type="text" id="realName" name="realName" class="form-control" required minlength="2" maxlength="20" value="${loginUser.realName}" placeholder="请输入您的真实姓名">
                </div>
                <div class="form-group">
                    <label for="idcard">身份证号：<span class="required-field">*</span></label>
                    <input type="text" id="idcard" name="idcard" class="form-control" required minlength="15" maxlength="18" value="${loginUser.idcard}" placeholder="请输入您的身份证号码">
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="sex">性别：<span class="required-field">*</span></label>
                    <select id="sex" name="sex" class="form-control" required>
                        <option value="0" ${loginUser.sex == 0 ? 'selected' : ''}>未设置</option>
                        <option value="1" ${loginUser.sex == 1 ? 'selected' : ''}>男</option>
                        <option value="2" ${loginUser.sex == 2 ? 'selected' : ''}>女</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="birthday">出生日期：<span class="required-field">*</span></label>
                    <input type="date" id="birthday" name="birthday" class="form-control" required value="${loginUser.birthday}">
                </div>
            </div>
            
            <div class="form-group">
                <label for="email">邮箱：</label>
                <input type="email" id="email" name="email" class="form-control" value="${loginUser.email}" placeholder="请输入您的邮箱地址">
            </div>
            
            <div class="form-group">
                <label for="phone">手机号：<span class="required-field">*</span></label>
                <input type="tel" id="phone" name="phone" class="form-control" required pattern="1[3-9]\d{9}" value="${loginUser.phone}" placeholder="请输入您的手机号码" readonly>
            </div>

            <button type="submit" class="btn-submit">保存个人信息</button>
        </form>

        <script>
            // 表单验证
            document.getElementById('profileForm').addEventListener('submit', function(e) {
                const realName = document.getElementById('realName').value;
                const idcard = document.getElementById('idcard').value;
                const email = document.getElementById('email').value;
                const birthday = document.getElementById('birthday').value;
                const sex = document.getElementById('sex').value;
                
                // 真实姓名验证
                if (realName.length < 2 || realName.length > 20) {
                    alert('真实姓名长度应在2-20个字符之间');
                    e.preventDefault();
                    return;
                }
                
                // 身份证号验证
                const idcardRegex = /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$/;
                if (!idcardRegex.test(idcard)) {
                    alert('请输入有效的身份证号码');
                    e.preventDefault();
                    return;
                }
                
                // 邮箱验证（如果填写了）
                if (email && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
                    alert('请输入有效的邮箱地址');
                    e.preventDefault();
                    return;
                }
                
                // 性别验证
                if (sex == 0) {
                    alert('请选择性别');
                    e.preventDefault();
                    return;
                }
                
                // 出生日期验证
                if (!birthday) {
                    alert('请选择出生日期');
                    e.preventDefault();
                    return;
                }
            });
        </script>
    </div>
</body>
</html>